package com.dyh.quickdemo.bizDemo.fillnode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * DeliveryorderCreateConvert的特殊逻辑补充
 */
@Component
public class DeliveryorderCreateConvertSpecial {

	@Autowired
	LvmhDynamicDsl lvmhDynamicDsl;


	List<WmsTypeSpecialFiller<?> > wmsTypeSpecialFillers = new ArrayList<>();


	public void fillSpecial(WmsOrderDto wmsOrderDto,DeliveryOrder deliveryOrder){
		for (WmsTypeSpecialFiller wmsTypeSpecialFiller : wmsTypeSpecialFillers) {
			if (wmsTypeSpecialFiller.matcher.test(wmsOrderDto.getWmsType())){
				wmsTypeSpecialFiller.setter.accept(deliveryOrder,wmsTypeSpecialFiller.mapper.apply(wmsOrderDto) );
			}
		}
	}


	@PostConstruct
	public void postConstruct(){
		wmsTypeSpecialFillers.add(
				WmsTypeSpecialFiller.of(e->true, lvmhDynamicDsl::fillShopNick,DeliveryOrder::setShopNick)
		);
	}

	static class WmsTypeSpecialFiller<T>{
		Predicate<String> matcher;
		Function<WmsOrderDto,T > mapper;
		BiConsumer<DeliveryOrder,T> setter;

		public static <T> WmsTypeSpecialFiller<T> of(Predicate<String> matcher,Function<WmsOrderDto,T> mapper,BiConsumer<DeliveryOrder, T> setter){
			WmsTypeSpecialFiller wmsTypeSpecialFiller = new WmsTypeSpecialFiller();
			wmsTypeSpecialFiller.mapper = mapper;
			wmsTypeSpecialFiller.matcher = matcher;
			wmsTypeSpecialFiller.setter = setter;
			return wmsTypeSpecialFiller;
		}
	}

}
